home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / des / des.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  13KB  |  522 lines

  1. /* Sofware DES functions
  2.  * written 12 Dec 1986 by Phil Karn, KA9Q; large sections adapted from
  3.  * the 1977 public-domain program by Jim Gillogly
  4.  */
  5. #define NULL    0
  6.  
  7. #ifdef    M_LITTLE_ENDIAN
  8. static unsigned long byteswap();
  9. #endif
  10.  
  11. /* Tables defined in the Data Encryption Standard documents */
  12.  
  13. /* initial permutation IP */
  14. static char ip[] = {
  15.     58, 50, 42, 34, 26, 18, 10,  2,
  16.     60, 52, 44, 36, 28, 20, 12,  4,
  17.     62, 54, 46, 38, 30, 22, 14,  6,
  18.     64, 56, 48, 40, 32, 24, 16,  8,
  19.     57, 49, 41, 33, 25, 17,  9,  1,
  20.     59, 51, 43, 35, 27, 19, 11,  3,
  21.     61, 53, 45, 37, 29, 21, 13,  5,
  22.     63, 55, 47, 39, 31, 23, 15,  7
  23. };
  24.  
  25. /* final permutation IP^-1 */
  26. static char fp[] = {
  27.     40,  8, 48, 16, 56, 24, 64, 32,
  28.     39,  7, 47, 15, 55, 23, 63, 31,
  29.     38,  6, 46, 14, 54, 22, 62, 30,
  30.     37,  5, 45, 13, 53, 21, 61, 29,
  31.     36,  4, 44, 12, 52, 20, 60, 28,
  32.     35,  3, 43, 11, 51, 19, 59, 27,
  33.     34,  2, 42, 10, 50, 18, 58, 26,
  34.     33,  1, 41,  9, 49, 17, 57, 25
  35. };
  36.  
  37. /* expansion operation matrix
  38.  * This is for reference only; it is unused in the code
  39.  * as the f() function performs it implicitly for speed
  40.  */
  41. #ifdef notdef
  42. static char ei[] = {
  43.     32,  1,  2,  3,  4,  5,
  44.      4,  5,  6,  7,  8,  9,
  45.      8,  9, 10, 11, 12, 13,
  46.     12, 13, 14, 15, 16, 17,
  47.     16, 17, 18, 19, 20, 21,
  48.     20, 21, 22, 23, 24, 25,
  49.     24, 25, 26, 27, 28, 29,
  50.     28, 29, 30, 31, 32,  1 
  51. };
  52. #endif
  53.  
  54. /* permuted choice table (key) */
  55. static char pc1[] = {
  56.     57, 49, 41, 33, 25, 17,  9,
  57.      1, 58, 50, 42, 34, 26, 18,
  58.     10,  2, 59, 51, 43, 35, 27,
  59.     19, 11,  3, 60, 52, 44, 36,
  60.  
  61.     63, 55, 47, 39, 31, 23, 15,
  62.      7, 62, 54, 46, 38, 30, 22,
  63.     14,  6, 61, 53, 45, 37, 29,
  64.     21, 13,  5, 28, 20, 12,  4
  65. };
  66.  
  67. /* number left rotations of pc1 */
  68. static char totrot[] = {
  69.     1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
  70. };
  71.  
  72. /* permuted choice key (table) */
  73. static char pc2[] = {
  74.     14, 17, 11, 24,  1,  5,
  75.      3, 28, 15,  6, 21, 10,
  76.     23, 19, 12,  4, 26,  8,
  77.     16,  7, 27, 20, 13,  2,
  78.     41, 52, 31, 37, 47, 55,
  79.     30, 40, 51, 45, 33, 48,
  80.     44, 49, 39, 56, 34, 53,
  81.     46, 42, 50, 36, 29, 32
  82. };
  83.  
  84. /* The (in)famous S-boxes */
  85. static char si[8][64] = {
  86.     /* S1 */
  87.     14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,
  88.      0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,
  89.      4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,
  90.     15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13,
  91.  
  92.     /* S2 */
  93.     15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
  94.      3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
  95.      0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
  96.     13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9,
  97.  
  98.     /* S3 */
  99.     10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,
  100.     13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,
  101.     13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,
  102.      1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12,
  103.  
  104.     /* S4 */
  105.      7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
  106.     13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
  107.     10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
  108.      3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14,
  109.  
  110.     /* S5 */
  111.      2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
  112.     14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
  113.      4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
  114.     11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3,
  115.  
  116.     /* S6 */
  117.     12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
  118.     10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
  119.      9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
  120.      4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13,
  121.  
  122.     /* S7 */
  123.      4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
  124.     13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
  125.      1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
  126.      6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12,
  127.  
  128.     /* S8 */
  129.     13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
  130.      1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
  131.      7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
  132.      2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11
  133. };
  134.  
  135. /* 32-bit permutation function P used on the output of the S-boxes */
  136. static char p32i[] = {    
  137.     16,  7, 20, 21,
  138.     29, 12, 28, 17,
  139.      1, 15, 23, 26,
  140.      5, 18, 31, 10,
  141.      2,  8, 24, 14,
  142.     32, 27,  3,  9,
  143.     19, 13, 30,  6,
  144.     22, 11,  4, 25
  145. };
  146. /* End of DES-defined tables */
  147.  
  148. /* Lookup tables initialized once only at startup by desinit() */
  149. static long (*sp_)[64];     /* Combined S and P boxes */
  150.  
  151. static char (*iperm)[16][8];    /* Initial and final permutations */
  152. static char (*fperm)[16][8];
  153.  
  154. /* 8 6-bit subkeys for each of 16 rounds, initialized by setkey() */
  155. static unsigned char (*kn)[8];
  156.  
  157. /* bit 0 is left-most in byte */
  158. static int bytebit[] = {
  159.     0200,0100,040,020,010,04,02,01
  160. };
  161.  
  162. static int nibblebit[] = {
  163.      010,04,02,01
  164. };
  165. static int desmode;
  166.  
  167. /* initialize a perm array */
  168. static
  169. perminit(perm,p)
  170. char perm[16][16][8];            /* 64-bit, either init or final */
  171. char p[64];
  172. {
  173.     register int l, j, k;
  174.     int i,m;
  175.  
  176.     /* Clear the permutation array */
  177.     for (i=0; i<16; i++)
  178.         for (j=0; j<16; j++)
  179.             for (k=0; k<8; k++)
  180.                 perm[i][j][k]=0;
  181.  
  182.     for (i=0; i<16; i++)        /* each input nibble position */
  183.         for (j = 0; j < 16; j++)/* each possible input nibble */
  184.         for (k = 0; k < 64; k++)/* each output bit position */
  185.         {   l = p[k] - 1;    /* where does this bit come from*/
  186.             if ((l >> 2) != i)  /* does it come from input posn?*/
  187.             continue;    /* if not, bit k is 0     */
  188.             if (!(j & nibblebit[l & 3]))
  189.             continue;    /* any such bit in input? */
  190.             m = k & 07;    /* which bit is this in the byte*/
  191.             perm[i][j][k>>3] |= bytebit[m];
  192.         }
  193. }
  194.  
  195. /* Initialize the lookup table for the combined S and P boxes */
  196. static int
  197. spinit()
  198. {
  199.     char pbox[32];
  200.     int p,i,s,j,rowcol;
  201.     long val;
  202.  
  203.     /* Compute pbox, the inverse of p32i.
  204.      * This is easier to work with
  205.      */
  206.     for(p=0;p<32;p++){
  207.         for(i=0;i<32;i++){
  208.             if(p32i[i]-1 == p){
  209.                 pbox[p] = i;
  210.                 break;
  211.             }
  212.         }
  213.     }
  214.     for(s = 0; s < 8; s++){         /* For each S-box */
  215.         for(i=0; i<64; i++){        /* For each possible input */
  216.             val = 0;
  217.             /* The row number is formed from the first and last
  218.              * bits; the column number is from the middle 4
  219.              */
  220.             rowcol = (i & 32) | ((i & 1) ? 16 : 0) | ((i >> 1) & 0xf);
  221.             for(j=0;j<4;j++){    /* For each output bit */
  222.                 if(si[s][rowcol] & (8 >> j)){
  223.                  val |= 1L << (31 - pbox[4*s + j]);
  224.                 }
  225.             }
  226.             sp_[s][i] = val;
  227.  
  228. #ifdef    DEBUG
  229.                         printf("sp_[%d][%2d] = %08lx\n",s,i,sp_[s][i]);
  230. #endif
  231.         }
  232.     }
  233. }
  234.  
  235.  
  236. /* Allocate space and initialize DES lookup arrays
  237.  * mode == 0: standard Data Encryption Algorithm
  238.  * mode == 1: DEA without initial and final permutations for speed
  239.  * mode == 2: DEA without permutations and with 128-byte key (completely
  240.  *          independent subkeys for each round)
  241.  */
  242. desinit(mode)
  243. int mode;
  244. {
  245.     char *malloc();
  246.  
  247.     if(sp_ != NULL){
  248.         /* Already initialized */
  249.         return 0;
  250.     }
  251.     desmode = mode;
  252.     
  253.     if((sp_ = (long (*)[64])malloc(sizeof(long) * 8 * 64)) == NULL){
  254.         return -1;
  255.     }
  256.     spinit();
  257.     kn = (unsigned char (*)[8])malloc(sizeof(char) * 8 * 16);
  258.     if(kn == NULL){
  259.         free((char *)sp_);
  260.         return -1;
  261.     }
  262.     if(mode == 1 || mode == 2)    /* No permutations */
  263.         return 0;
  264.  
  265.     iperm = (char (*)[16][8])malloc(sizeof(char) * 16 * 16 * 8);
  266.     if(iperm == NULL){
  267.         free((char *)sp_);
  268.         free((char *)kn);
  269.         return -1;
  270.     }
  271.     perminit(iperm,ip);
  272.  
  273.     fperm = (char (*)[16][8])malloc(sizeof(char) * 16 * 16 * 8);
  274.     if(fperm == NULL){
  275.         free((char *)sp_);
  276.         free((char *)kn);
  277.         free((char *)iperm);
  278.         return -1;
  279.     }
  280.     perminit(fperm,fp);
  281.     
  282.     return 0;
  283. }
  284. /* Free up storage used by DES */
  285. desdone()
  286. {
  287.     if(sp_ == NULL)
  288.         return; /* Already done */
  289.  
  290.     free((char *)sp_);
  291.     free((char *)kn);
  292.     if(iperm != NULL)
  293.         free((char *)iperm);
  294.     if(fperm != NULL)
  295.         free((char *)fperm);
  296.  
  297.     sp_ = NULL;
  298.     iperm = NULL;
  299.     fperm = NULL;
  300.     kn = NULL;
  301. }
  302. /* Set key (initialize key schedule array) */
  303. setkey(key)
  304. char *key;            /* 64 bits (will use only 56) */
  305. {
  306.     char pc1m[56];        /* place to modify pc1 into */
  307.     char pcr[56];        /* place to rotate pc1 into */
  308.     register int i,j,l;
  309.     int m;
  310.  
  311.     /* In mode 2, the 128 bytes of subkey are set directly from the
  312.          * user's key, allowing him to use completely independent
  313.      * subkeys for each round. Note that the user MUST specify a
  314.      * full 128 bytes.
  315.      *
  316.      * I would like to think that this technique gives the NSA a real
  317.          * headache, but I'm not THAT naive.
  318.      */
  319.     if(desmode == 2){
  320.         for(i=0;i<16;i++)
  321.             for(j=0;j<8;j++)
  322.                 kn[i][j] = *key++;
  323.         return;
  324.     }
  325.     /* Clear key schedule */
  326.     for (i=0; i<16; i++)
  327.         for (j=0; j<8; j++)
  328.             kn[i][j]=0;
  329.  
  330.     for (j=0; j<56; j++) {        /* convert pc1 to bits of key */
  331.         l=pc1[j]-1;        /* integer bit location  */
  332.         m = l & 07;        /* find bit         */
  333.         pc1m[j]=(key[l>>3] &    /* find which key byte l is in */
  334.             bytebit[m])    /* and which bit of that byte */
  335.             ? 1 : 0;    /* and store 1-bit result */
  336.     }
  337.     for (i=0; i<16; i++) {        /* key chunk for each iteration */
  338.         for (j=0; j<56; j++)    /* rotate pc1 the right amount */
  339.             pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28];
  340.             /* rotate left and right halves independently */
  341.         for (j=0; j<48; j++){    /* select bits individually */
  342.             /* check bit that goes to kn[j] */
  343.             if (pcr[pc2[j]-1]){
  344.                                 /* mask it in if it's there */
  345.                 l= j % 6;
  346.                 kn[i][j/6] |= bytebit[l] >> 2;
  347.             }
  348.         }
  349.     }
  350. }
  351.  
  352.  
  353. /* Permute inblock with perm */
  354. static
  355. permute(inblock,perm,outblock)
  356. char *inblock, *outblock;        /* result into outblock,64 bits */
  357. char perm[16][16][8];            /* 2K bytes defining perm. */
  358. {
  359.     register int i,j;
  360.     register char *ib, *ob;     /* ptr to input or output block */
  361.     register char *p, *q;
  362.  
  363.     if(perm == NULL){
  364.         /* No permutation, just copy */
  365.         for(i=8; i!=0; i--)
  366.             *outblock++ = *inblock++;
  367.         return;
  368.     }
  369.     /* Clear output block     */
  370.     for (i=8, ob = outblock; i != 0; i--)
  371.         *ob++ = 0;
  372.  
  373.     ib = inblock;
  374.     for (j = 0; j < 16; j += 2, ib++) { /* for each input nibble */
  375.         ob = outblock;
  376.         p = perm[j][(*ib >> 4) & 017];
  377.         q = perm[j + 1][*ib & 017];
  378.         for (i = 8; i != 0; i--){   /* and each output byte */
  379.             *ob++ |= *p++ | *q++;    /* OR the masks together*/
  380.         }
  381.     }
  382. }
  383. /* The nonlinear function f(r,k), the heart of DES */
  384. static
  385. long
  386. f(r,subkey)
  387. unsigned long r;        /* 32 bits */
  388. unsigned char subkey[8];    /* 48-bit key for this round */
  389. {
  390.     register unsigned long rval,rt;
  391. #ifdef    TRACE
  392.     unsigned char *cp;
  393.     int i;
  394.  
  395.         printf("f(%08lx, %02x %02x %02x %02x %02x %02x %02x %02x) = ",
  396.         r,
  397.         subkey[0], subkey[1], subkey[2],
  398.         subkey[3], subkey[4], subkey[5],
  399.         subkey[6], subkey[7]);
  400. #endif
  401.     /* Run E(R) ^ K through the combined S & P boxes
  402.      * This code takes advantage of a convenient regularity in
  403.      * E, namely that each group of 6 bits in E(R) feeding
  404.      * a single S-box is a contiguous segment of R.
  405.      */
  406.     rt = (r >> 1) | ((r & 1) ? 0x80000000 : 0);
  407.     rval = 0;
  408.     rval |= sp_[0][((rt >> 26) ^ *subkey++) & 0x3f];
  409.     rval |= sp_[1][((rt >> 22) ^ *subkey++) & 0x3f];
  410.     rval |= sp_[2][((rt >> 18) ^ *subkey++) & 0x3f];
  411.     rval |= sp_[3][((rt >> 14) ^ *subkey++) & 0x3f];
  412.     rval |= sp_[4][((rt >> 10) ^ *subkey++) & 0x3f];
  413.     rval |= sp_[5][((rt >> 6) ^ *subkey++) & 0x3f];
  414.     rval |= sp_[6][((rt >> 2) ^ *subkey++) & 0x3f];
  415.     rt = (r << 1) | ((r & 0x80000000) ? 1 : 0);
  416.     rval |= sp_[7][(rt ^ *subkey) & 0x3f];
  417. #ifdef    TRACE
  418.         printf(" %08lx\n",rval);
  419. #endif
  420.     return rval;
  421. }
  422.  
  423. /* Do one DES cipher round */
  424. static
  425. round(num,block)
  426. int num;                /* i.e. the num-th one     */
  427. unsigned long *block;
  428. {
  429.     long f();
  430.  
  431.     /* The rounds are numbered from 0 to 15. On even rounds
  432.      * the right half is fed to f() and the result exclusive-ORs
  433.      * the left half; on odd rounds the reverse is done.
  434.      */
  435.     if(num & 1){
  436.         block[1] ^= f(block[0],kn[num]);
  437.     } else {
  438.         block[0] ^= f(block[1],kn[num]);
  439.     }
  440. }
  441. /* In-place encryption of 64-bit block */
  442. endes(block)
  443. char *block;
  444. {
  445.     register int i;
  446.     unsigned long work[2];        /* Working data storage */
  447.     long tmp;
  448.  
  449.     permute(block,iperm,(char *)work);    /* Initial Permutation */
  450. #ifdef M_LITTLE_ENDIAN
  451.     work[0] = byteswap(work[0]);
  452.     work[1] = byteswap(work[1]);
  453. #endif
  454.  
  455.     /* Do the 16 rounds */
  456.     for (i=0; i<16; i++)
  457.         round(i,work);
  458.  
  459.     /* Left/right half swap */
  460.     tmp = work[0];
  461.     work[0] = work[1];    
  462.     work[1] = tmp;
  463.  
  464. #ifdef M_LITTLE_ENDIAN
  465.     work[0] = byteswap(work[0]);
  466.     work[1] = byteswap(work[1]);
  467. #endif
  468.     permute((char *)work,fperm,block);    /* Inverse initial permutation */
  469. }
  470. /* In-place decryption of 64-bit block */
  471. dedes(block)
  472. char *block;
  473. {
  474.     register int i;
  475.     unsigned long work[2];    /* Working data storage */
  476.     long tmp;
  477.  
  478.     permute(block,iperm,(char *)work);    /* Initial permutation */
  479.  
  480. #ifdef M_LITTLE_ENDIAN
  481.     work[0] = byteswap(work[0]);
  482.     work[1] = byteswap(work[1]);
  483. #endif
  484.  
  485.     /* Left/right half swap */
  486.     tmp = work[0];
  487.     work[0] = work[1];    
  488.     work[1] = tmp;
  489.  
  490.     /* Do the 16 rounds in reverse order */
  491.     for (i=15; i >= 0; i--)
  492.         round(i,work);
  493.  
  494. #ifdef M_LITTLE_ENDIAN
  495.     work[0] = byteswap(work[0]);
  496.     work[1] = byteswap(work[1]);
  497. #endif
  498.  
  499.     permute((char *)work,fperm,block);    /* Inverse initial permutation */
  500. }
  501. #ifdef    M_LITTLE_ENDIAN
  502. /* Byte swap a long */
  503. static
  504. unsigned long
  505. byteswap(x)
  506. unsigned long x;
  507. {
  508.     register char *cp,tmp;
  509.  
  510.     cp = (char *)&x;
  511.     tmp = cp[3];
  512.     cp[3] = cp[0];
  513.     cp[0] = tmp;
  514.  
  515.     tmp = cp[2];
  516.     cp[2] = cp[1];
  517.     cp[1] = tmp;
  518.  
  519.     return x;
  520. }
  521. #endif
  522.